import { _decorator, Component, Node, tween, v3, Vec3 } from "cc";
const { ccclass, property } = _decorator;

@ccclass("ShaderCaseMoveRole")
export class ShaderCaseMoveRole extends Component {
    @property(Node)
    nodeRole: Node = null;

    private _posList = [v3(-3.5, 0, -3.5), v3(0, 0, 1.5), v3(1.7, 0, -1.3)];
    private _index = 0;
    private _speed = 1.5;

    protected onLoad(): void {
        if (!this.nodeRole) return;
        this.nodeRole.setPosition(this._posList[0]);
        this._moveToNextPos();
    }

    /**
     * 前往下一目标地点
     */
    private _moveToNextPos(): void {
        this._index = (this._index + 1) % 3;
        const pos = this._posList[this._index];
        const dis = Vec3.distance(pos, this.nodeRole.position);
        const time = dis / this._speed;
        const forward = this.nodeRole.position.clone().subtract(pos);
        tween(this.nodeRole)
            .to(0.4, { forward: forward })
            .to(time, { position: pos })
            .call(() => {
                this._moveToNextPos();
            })
            .start();
    }
}
